package org.edu.modules.stu.jis.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.dto.ExcelPo;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.jis.dto.VeStuIdxScoreConfigDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.jis.entity.VeStuIdxScoreConfig;
import org.edu.modules.stu.jis.service.VeStuIdxScoreConfigService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"指标加减分设置表"})
@RestController
@RequestMapping("/stu/veStuIdxScoreConfig")
@Slf4j
public class VeStuIdxScoreConfigController {

    @Autowired
    private VeStuIdxScoreConfigService veStuIdxScoreConfigService;

    @ApiOperation("分页查询指标加减分设置表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeStuIdxScoreConfig> page) {
        QueryWrapper<VeStuIdxScoreConfig> queryWrapper = page.buildQueryWrapper(VeStuIdxScoreConfig.class);
        return Result.OK(veStuIdxScoreConfigService.page(page, queryWrapper));
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @PostMapping("/all")
    public Result<?> all(@RequestBody AllDto<VeStuIdxScoreConfig> page) {
        //QueryWrapper<VeStuIdxScoreConfig> queryWrapper = page.buildQueryWrapper(VeStuIdxScoreConfig.class);
        List<VeStuIdxScoreConfigDto> list = veStuIdxScoreConfigService.getAll();
        return Result.OK(list);
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeStuIdxScoreConfig> page, HttpServletResponse resp) {
        QueryWrapper<VeStuIdxScoreConfig> queryWrapper = page.buildQueryWrapper(VeStuIdxScoreConfig.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("指标ID", "idxId"));
        list.add(new ExcelPo("指标名称", "name"));
        list.add(new ExcelPo("指标类型名称", "attrName"));
        list.add(new ExcelPo("指标类型", "attribute"));
        list.add(new ExcelPo("指标加分", "addScore"));
        list.add(new ExcelPo("指标减分", "minusScore"));
        list.add(new ExcelPo("加分单位类型", "addType"));
        list.add(new ExcelPo("减分单位类型", "minType"));
        list.add(new ExcelPo("分值单位", "addUnit"));
        list.add(new ExcelPo("减分单位", "minUnit"));
        list.add(new ExcelPo("排序", "listSort"));
        PoiUtils.downloadFile(resp, veStuIdxScoreConfigService.page(page, queryWrapper).getRecords(), list);
        return Result.OK();
    }

    @AutoLog(value = "指标加减分设置表-分页列表查询")
    @ApiOperation(value = "指标加减分设置表-分页列表查询", notes = "指标加减分设置表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeStuIdxScoreConfig entity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeStuIdxScoreConfig> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeStuIdxScoreConfig> page = new PageDto<VeStuIdxScoreConfig>(pageNo, pageSize);
        IPage<VeStuIdxScoreConfig> pageList = veStuIdxScoreConfigService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeStuIdxScoreConfig dto = veStuIdxScoreConfigService.findDtoById(id);
        if (dto == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeStuIdxScoreConfigDto dto) {
        veStuIdxScoreConfigService.insertOrUpdate(dto, true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody List<VeStuIdxScoreConfigDto> list) {
        // 处理 VeStuIdxScoreConfigDto 对象列表
        for (VeStuIdxScoreConfigDto dto : list) {
            veStuIdxScoreConfigService.insertOrUpdate(dto, false);
        }
        return Result.OK(null);
    }


    @AutoLog(value = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        veStuIdxScoreConfigService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value = "更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        LambdaUpdateWrapper<VeStuIdxScoreConfig> updateWrapper = new LambdaUpdateWrapper<>();
        if (statusDto.getIds() != null && statusDto.getIds().size() > 0) {
            //     updateWrapper.set(VeStuIdxScoreConfig::getStatus,statusDto.getStatus());
            updateWrapper.in(VeStuIdxScoreConfig::getId, statusDto.getIds());
            veStuIdxScoreConfigService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        } else {
            return Result.error("未选择!");
        }
    }


}
